package com.clayecloud.module_property_payment.viewModels;

import android.util.Log;

import androidx.annotation.NonNull;
import androidx.lifecycle.MutableLiveData;
import androidx.lifecycle.ViewModel;

import com.clayecloud.lib_base.action.ToastAction;
import com.clayecloud.lib_common.apis.HouseoOrderApi;
import com.clayecloud.lib_common.utils.GsonUtil;
import com.clayecloud.lib_common.utils.HttpUtil;
import com.clayecloud.lib_common.utils.LoginUtil;
import com.clayecloud.lib_common.utils.MessageUtil;
import com.clayecloud.module_property_payment.entity.HouseOrder;
import com.google.gson.reflect.TypeToken;

import java.util.ArrayList;
import java.util.List;

import io.reactivex.Observer;
import io.reactivex.android.schedulers.AndroidSchedulers;
import io.reactivex.disposables.Disposable;
import io.reactivex.schedulers.Schedulers;

public class PropertyPaymentViewModel extends ViewModel implements ToastAction {

    private HouseoOrderApi houseoOrderApi;

    private MutableLiveData<List<HouseOrder>> houseOrders;

    public MutableLiveData<List<HouseOrder>> getHouseOrders() {
        return houseOrders;
    }

    public void setHouseOrders(MutableLiveData<List<HouseOrder>> houseOrders) {
        this.houseOrders = houseOrders;
    }

    public void leadDate(){
        houseoOrderApi = HttpUtil.getInstance().create(HouseoOrderApi.class);
        MutableLiveData<List<HouseOrder>> houseOrders = new MutableLiveData<>();
        houseOrders.setValue(new ArrayList<>());
        setHouseOrders(houseOrders);
        initHouseOrderData();
    }

    private void initHouseOrderData() {
        houseoOrderApi.getUserAllNoPayOrder(LoginUtil.getUserID())
                .subscribeOn(Schedulers.newThread())
                .observeOn(AndroidSchedulers.mainThread())
                .subscribe(new Observer<MessageUtil>() {
                    private Disposable disposable;

                    @Override
                    public void onSubscribe(@NonNull Disposable d) {
                        disposable = d;
                    }

                    @Override
                    public void onNext(@NonNull MessageUtil messageUtil) {
                        if(messageUtil.getResult()){
                            List<HouseOrder> houseOrderList = GsonUtil.getInstance().fromJson(GsonUtil.getInstance().toJson(messageUtil.getObj()),new TypeToken<List<HouseOrder>>() {}.getType());
                            MutableLiveData<List<HouseOrder>> houseOrders = getHouseOrders();
                            houseOrders.setValue(houseOrderList);
                            setHouseOrders(houseOrders);
                        }else{
                            toast(messageUtil.getMsg());
                        }
                    }

                    @Override
                    public void onError(@NonNull Throwable e) {
                        toast("网络请求失败，请联系管理员");
                        Log.e("PropertyPaymentViewModel initHouseOrderData error", e.getMessage());
                        disposable.dispose();
                    }

                    @Override
                    public void onComplete() {
                        disposable.dispose();
                    }
                });


    }

}
